package cn.springBoot.controller;


import org.camunda.bpm.engine.FormService;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.form.FormField;
import org.camunda.bpm.engine.form.FormProperty;
import org.camunda.bpm.engine.form.StartFormData;
import org.camunda.bpm.engine.form.TaskFormData;
import org.camunda.bpm.engine.repository.Deployment;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.task.Task;
import org.camunda.bpm.engine.variable.VariableMap;
import org.camunda.bpm.engine.variable.Variables;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author ljx
 * @description:  表单操作
 * @class cn.springBoot.controller.FormController
 * @create date: 2021/9/27
 */
@RestController
@RequestMapping("/form")
public class FormController {


    @Autowired
    FormService formService;
    @Autowired
    RepositoryService repositoryService;
    @Autowired
    TaskService taskService;

    @GetMapping(value ="/createDefinition" )
    public  boolean  createDefinition(String name ,String url) throws IOException {


        File f = new File("D:/localGit/springbootBeginner/src/main/resources/diagram/test.bpmn");
        File from1 = new File("D:/localGit/springbootBeginner/src/main/resources/static/test.html");


        InputStream inputStreambpmn=new FileInputStream(f);
        InputStream inputStreamForm=new FileInputStream(from1);
      //  InputStream inputStreamForm2=new FileInputStream(from2);
        Deployment deployment = repositoryService// 与流程定义和部署对象相关的sevices
                .createDeployment()// 创建一个部署对象
                .name(name)// 添加部署名称
                .addInputStream("test.bpmn", inputStreambpmn)
                .addInputStream("test.html",inputStreamForm)
             //   .addInputStream("test2.html",inputStreamForm2)
                .deploy();// 完成部署
        System.out.println("部署ID：" + deployment.getId());
        System.out.println("部署名称" + deployment.getName());

        inputStreambpmn.close();
        return true;
    }

    /**
     * @description: 获取 开始节点 表单 formkey  （外置表单）
     * @param:key
     * @author: ljx
     * @create date: 2021/9/27
     * @version: V1.0
     * @return: void
     */

    @GetMapping("/getStartFormKey")
    public  void  getStartFormKey(String processDefinitionId){

        String startFormKey = formService.getStartFormKey(processDefinitionId);

        System.out.println("startFormKey:"+startFormKey);

    }
    /**
     * @description:  获取 任务节点 表单 form key（外置表单）
     * @param:processDefinitionId
     * @Param: taskDefinitionKey xml节点 id 值  userTask id="Activity_135kvlx“
     * @author: ljx
     * @create date: 2021/9/27
     * @version: V1.0
     * @return: void
     */

    @GetMapping("/getTaskFormKey")
    public  void  getStartFormKey(String processDefinitionId, String taskDefinitionKey){

        String taskFormKey = formService.getTaskFormKey(processDefinitionId, taskDefinitionKey);

        System.out.println("taskFormKey:"+taskFormKey);

    }
    /** 
     * @description:  获取 开始节点 from  （外置表单）
     * @param:processDefinitionId 
     * @author: ljx
     * @create date: 2021/9/27 
     * @version: V1.0 
     * @return: void 
     */
     
    @GetMapping("/getRenderedStartForm")
    public  void  getRenderedStartForm(String processDefinitionId){

        Object renderedStartForm = formService.getRenderedStartForm(processDefinitionId,"juel");

        System.out.println("renderedStartForm:"+renderedStartForm);

    }
    /**
     * @description:  获取 任务节点 form （外置表单）
     * @param:taskId
     * @author: ljx
     * @create date: 2021/9/27
     * @version: V1.0
     * @return: void
     */

    @GetMapping("/getRenderedTaskForm")
    public  void  getRenderedTaskForm(String taskId){

        Object renderedTaskForm = formService.getRenderedTaskForm(taskId, "juel");

        System.out.println("############");
        System.out.println(renderedTaskForm);
        System.out.println("############");

    }
    
    /** 
     * @description: 提交 开始节点 表单
     * @param:
     * @author: ljx
     * @create date: 2021/9/27 
     * @version: V1.0 
     * @return: void 
     */
     
    public void submitStartForm() {
        String processDefinitionId = "form:3:5306";
        VariableMap variableMap = Variables.createVariables()
                .putValue("days", 10)
                .putValue("reason", "请假三天")
                .putValue("startUserId", "张三")
                .putValue("category", "年假");
        ProcessInstance processInstance = formService.submitStartForm(processDefinitionId, variableMap);
        System.out.println(processInstance);
    }

    
    /** 
     * @description: 提交 任务节点 表单
     * @param:taskId 
     * @author: ljx
     * @create date: 2021/9/27 
     * @version: V1.0 
     * @return: void 
     */
     
     @GetMapping("/submitTaskForm")
    public void submitTaskForm(String taskId) {
        VariableMap variableMap = Variables.createVariables()
                .putValue("startUserId", "王五")
                .putValue("category", "年假");

        formService.submitTaskForm(taskId, variableMap);
    }


    /**
     * @description:  获取 动态表单 任务节点 数据
     * @param:taskId
     * @author: ljx
     * @create date: 2021/9/27
     * @version: V1.0
     * @return: void
     */

    @GetMapping("/getTaskFormData")
    public void getTaskFormData(String taskId ) {

        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        System.out.println(task.getId());

        TaskFormData taskFormData = formService.getTaskFormData(task.getId());
        System.out.println("############");
        System.out.println(taskFormData.getDeploymentId());
        System.out.println(taskFormData.getTask());
        System.out.println(taskFormData.getFormKey());
        List<FormProperty> formProperties = taskFormData.getFormProperties();
        System.out.println(formProperties);
        List<FormField> formFields = taskFormData.getFormFields();
        System.out.println(formFields);
        System.out.println("############");
    }

    /**
     * @description: 动态表单 开始节点 数据
     * @param:processDefinitionId
     * @author: ljx
     * @create date: 2021/9/27
     * @version: V1.0
     * @return: void
     */

    @GetMapping("/getStartFormData")
    public void getStartFormData( String processDefinitionId) {

        StartFormData startFormData = formService.getStartFormData(processDefinitionId);
        List<FormField> formFields = startFormData.getFormFields();
        for (FormField formField : formFields) {
            System.out.println("############");
            System.out.println(formField.getId());
            System.out.println(formField.getValue());
            System.out.println(formField.getType());
            System.out.println(formField.getTypeName());
            System.out.println(formField.getLabel());
            System.out.println(formField.getDefaultValue());
            System.out.println("############");
        }
    }



}

